﻿package Engine.RMI
{
    import Framework.Protocol.*;

    public class CommunicatorManager extends Object
    {
        private var _communicators:Array;
        private static var instance:CommunicatorManager;

        public function CommunicatorManager()
        {
            if (instance != null)
            {
                throw new Error("singler");
            }
            this._communicators = new Array();
        }

        public function createCommunicator(url:String, protocol:IProtocol = null) : Communicator
        {
            var communicator:Communicator = null;
            var i:int = 0;
            while (i < this._communicators.length)
            {
                
				communicator = this._communicators[i] as Communicator;
                if (communicator.url == url)
                {
                    return communicator;
                }
                i++;
            }
			communicator = new Communicator(protocol);
			communicator.url = url;
            this._communicators.push(communicator);
            return communicator;
        }

        public function removeCommunicator(url:String) : Boolean
        {
            var communicator:Communicator = null;
            var i:int = 0;
            var bol:Boolean = false;
            i = 0;
            while (i < this._communicators.length)
            {
                
                communicator = this._communicators[i] as Communicator;
                if (communicator.url == url)
                {
                    this._communicators.splice(i, 1);
					bol = true;
                }
                i++;
            }
			
            return bol;
        }

        public static function getInstance() : CommunicatorManager
        {
            if (instance == null)
            {
                instance = new CommunicatorManager;
            }
            return instance;
        }
    }
}
